﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.ComponentModel;

using Willson.JsbManager.Idal;
using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;

namespace Willson.JsbManager.Bll
{
    /// <summary>
    /// 系统查询逻辑层
    /// </summary>
    public class BLLXTQuery:BaseBLL
    {
        private ITableXtCompany xt_company_DAL = null;
        private ITableXtEmployee xt_employee_DAL = null;
        private ITableXtBaseBankinfo xt_base_bankinfo_DAL = null;
        private ITableXtDelegateBank xt_delegate_bank_DAL = null;
        private ITable_xt_waterproperty xt_waterproperty_DAL = null;
        private ITableXtCompany_sb_info xt_company_sb_info_DAL = null;
        private ITableXtSbMonth xt_sb_month_DAL = null;
        private ITableXtSbJd xt_sb_jd_DAL = null;
        
        public BLLXTQuery()
        {
            xt_company_DAL = Reflect<ITableXtCompany>.Create("DAL_table_xt_company", "Willson.JsbManager.Dal");
            xt_employee_DAL = Reflect<ITableXtEmployee>.Create("DAL_table_xt_employee", "Willson.JsbManager.Dal");
            xt_base_bankinfo_DAL = Reflect<ITableXtBaseBankinfo>.Create("DAL_table_xt_base_bankinfo", "Willson.JsbManager.Dal");
            xt_delegate_bank_DAL = Reflect<ITableXtDelegateBank>.Create("DAL_table_xt_delegate_bank", "Willson.JsbManager.Dal");
            xt_waterproperty_DAL = Reflect<ITable_xt_waterproperty>.Create("DAL_table_xt_waterproperty", "Willson.JsbManager.Dal");
            xt_company_sb_info_DAL = Reflect<ITableXtCompany_sb_info>.Create("DAL_table_xt_company_sb_info", "Willson.JsbManager.Dal");
            xt_sb_month_DAL = Reflect<ITableXtSbMonth>.Create("DAL_table_xt_sb_month", "Willson.JsbManager.Dal");
            xt_sb_jd_DAL = Reflect<ITableXtSbJd>.Create("DAL_table_xt_sb_jd", "Willson.JsbManager.Dal"); 
        }

        /// <summary>
        /// 查询季度用水信息
        /// </summary>
        /// <param name="company_id">单位ID号</param>
        /// <returns></returns>
        public BindingList<BEXTQuerySbJdInfo> GetXTQuerySbJdInfo(int company_id)
        {
            BindingList<BEXTQuerySbJdInfo> list = new BindingList<BEXTQuerySbJdInfo>();

            Hashtable hash = new Hashtable();

            hash.Add("company_id", company_id);

            IEnumerator<TableXtSbJdEntity> ator = xt_sb_jd_DAL.Find(hash, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                BEXTQuerySbJdInfo info = new BEXTQuerySbJdInfo();

                info.Sb_year = ator.Current.Sb_year;
                info.Sb_jd = ator.Current.Sb_jd;
                info.Jd_sum = ator.Current.Jd_sum;
                info.Jd_jh = ator.Current.Jd_jh;
                info.Jd_jh_begin = ator.Current.Jd_jh_begin;
                info.Jd_cjh = ator.Current.Jd_cjh;
                info.Jd_scjh = ator.Current.Jd_scjh;
                info.Jd_jh_add = ator.Current.Jd_jh_add;
                info.Jd_memo = ator.Current.Memo;

                list.Add(info);
            }

            return list;
        }


        /// <summary>
        /// 查询月用水信息 
        /// </summary>
        /// <param name="sb_id">水表ID号</param>
        /// <returns></returns>
        public BindingList<BEXTQuerySbMonthInfo> GetXTQuerySbMonthInfo(int sb_id)
        {
            BindingList<BEXTQuerySbMonthInfo> list = new BindingList<BEXTQuerySbMonthInfo>();
            
            Hashtable ht = new Hashtable();

            ht.Add("sb_id",sb_id);

            IEnumerator<Table_xt_sb_month_entity> ator = xt_sb_month_DAL.Find(ht, SqlOperator.And, true).GetEnumerator();

            while (ator.MoveNext())
            {
                BEXTQuerySbMonthInfo info = new BEXTQuerySbMonthInfo();

                if (ator.Current.Sb_cjs <= 0) continue;

                info.Sb_begin = ator.Current.Sb_begin;
                info.Sb_cjs = ator.Current.Sb_cjs;
                info.Sb_hb = ator.Current.Sb_hb;
                info.Sb_month = ator.Current.Sb_month;
                info.Sb_year = ator.Current.Sb_year;
                info.Memo = ator.Current.Memo;
                info.Sb_ysl = ator.Current.Sb_ysl;

                list.Add(info);
            }

            return list;
        }

        /// <summary>
        /// 水表信息查询
        /// </summary>
        /// <param name="company_id">单位信息ID</param>
        /// <returns></returns>
        public BindingList<BEXTQuerySbInfo> GetXTQuerySbInfo(int company_id)
        {
            BindingList<BEXTQuerySbInfo> list = new BindingList<BEXTQuerySbInfo>();
            Hashtable ht = new Hashtable();
            ht.Add("company_id",company_id);

            IEnumerator<TableXtCompanySbInfoEntity> ator = xt_company_sb_info_DAL.Find(ht, SqlOperator.And, false).GetEnumerator();

            while (ator.MoveNext())
            {
                BEXTQuerySbInfo info = new BEXTQuerySbInfo();
                
                Table_xt_waterproperty_entity wt = xt_waterproperty_DAL.FindByID(ator.Current.Wp_id);

                info.User_code = ator.Current.User_code;
                info.Ht_code = ator.Current.Ht_code;
                info.Money = string.Format("{0:0.00}",wt.Money);
                info.Sb_size = ator.Current.Sb_size;
                info.Water_name = wt.Name;
                info.Sb_memo = ator.Current.Memo;
                info.Sb_id = ator.Current.Id;

                if (ator.Current.Remove == 0)
                {
                    info.Sb_stats = "正常";
                }
                else
                {
                    info.Sb_stats = "停用";
                }

                list.Add(info);
            }

            return list;
        }


        /// <summary>
        /// 委托信息查询
        /// </summary>
        /// <param name="company_id">单位信息表ID</param>
        /// <returns></returns>
        public BEXTQueryDelegateBankInfo GetXTQueryDelegateBankInfo(int company_id)
        {
            BEXTQueryDelegateBankInfo info = new BEXTQueryDelegateBankInfo();
            TableXtDelegateBankEntity delegate_bank_entity = null;
            TableXTBaseBankInfoEntity base_bankinfo_entity = null;
            Hashtable ht = new Hashtable();

            ht.Add("company_id",company_id);
            delegate_bank_entity = xt_delegate_bank_DAL.Find(ht);

            if (delegate_bank_entity == null) return null;

            base_bankinfo_entity = xt_base_bankinfo_DAL.FindByID(delegate_bank_entity.Bank_id);

            info.Account = delegate_bank_entity.Account;
            info.Bank_name = base_bankinfo_entity.Name;
            //info.Delegate_code = "DI" + delegate_bank_entity.Delegate_code;
            info.Delegate_code = delegate_bank_entity.Delegate_code;
            info.Memo = delegate_bank_entity.Memo;
            info.Name = delegate_bank_entity.Name;
            info.Name_tt = delegate_bank_entity.Name_tt;

            return info;
        }


        /// <summary>
        /// 主查询
        /// </summary>
        /// <param name="text">条件</param>
        /// <param name="type">方式</param>
        /// <returns></returns>
        public BindingList<BEXTQueryMainInfo> GetXTQueryMainInfo(string text, QueryCompanyType type)
        {
            BindingList<BEXTQueryMainInfo> list = new BindingList<BEXTQueryMainInfo>();
            Hashtable ht = new Hashtable();
            TableXtEmployeeEntity employee = null;

            if (type == QueryCompanyType.Pym)
            {
                ht.Add("pym", text);
            }

            if (type == QueryCompanyType.Name)
            {
                ht.Add("name", text);
            }

            IEnumerator<TableXtCompanyEntity> ator = xt_company_DAL.Find(ht, SqlOperator.Like, true).GetEnumerator();

            while (ator.MoveNext())
            {
                if (ator.Current.Remove == 1) continue;
                
                BEXTQueryMainInfo info = new BEXTQueryMainInfo();

                employee = xt_employee_DAL.FindByID(ator.Current.Gm);

                info.Company_id = ator.Current.Id;
                info.Company_name = ator.Current.Name;
                info.Company_address = ator.Current.Address;
                info.Company_telphone = ator.Current.Telphone;
                info.Company_gm_name = employee.Name;

                list.Add(info);

            }

            return list;
        }

        /// <summary>
        /// 单位信息查询
        /// </summary>
        /// <param name="company_id">单位信息表ID</param>
        /// <returns></returns>
        public BEXTQueryCompanyInfo GetXTQueryCompanyInfo(int company_id)
        {
            BEXTQueryCompanyInfo info = new BEXTQueryCompanyInfo();
            TableXtCompanyEntity company_entity = null;
            TableXtEmployeeEntity employee_entity = null;

            company_entity = xt_company_DAL.FindByID(company_id);
            employee_entity = xt_employee_DAL.FindByID(company_entity.Gm);

            info.Company_id = company_entity.Id;
            info.Company_pym = company_entity.Pym;
            info.Company_name = company_entity.Name;
            info.Company_address = company_entity.Address;
            info.Company_telphone = company_entity.Telphone;
            info.Company_memo = company_entity.Memo;
            info.Company_code = company_entity.Code;
            info.Company_sjr = company_entity.Sjr;
            info.Company_gm_name = employee_entity.Name;

            return info;
        }
    }
}
